package com.baiwei.vpnapi.config.cors;


import com.baiwei.vpnapi.filter.JwtTokenFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import javax.servlet.Filter;

/**
 * @program: vpnapi
 * @description: 跨域配置
 * @author: Mr. Stone
 * @create: 2020-03-12 19:00
 **/
@Configuration
public class CorsConfig {

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //放行哪些原始域
        config.addAllowedOrigin("*");
        config.addAllowedOrigin("http://23.97.70.51:8092");
        //是否发送Cookie信息
        config.setAllowCredentials(true);
        //放行哪些原始域(请求方式)
        config.addAllowedMethod("*");
        //放行哪些原始域(头部信息)
        config.addAllowedHeader("*");
        //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
        // Access-Control-Allow-Origin
        config.addExposedHeader("Content-Type");
        config.addExposedHeader("X-Requested-With");
        config.addExposedHeader("accept");
        config.addExposedHeader("Origin");
        config.addExposedHeader("Access-Control-Request-Method");
        config.addExposedHeader("Access-Control-Request-Headers");
        config.addExposedHeader("language");
        config.addExposedHeader("Authorization");
        config.addExposedHeader("platform");
        config.addExposedHeader("ip");
        config.addExposedHeader("device");
        config.addExposedHeader("version");
        //2.添加映射路径
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        CorsFilter corsFilter = new CorsFilter(configSource);
        filterFilterRegistrationBean.setFilter(corsFilter);
        filterFilterRegistrationBean.setOrder(-1);
        return filterFilterRegistrationBean;
    }

//    @Bean
//    public FilterRegistrationBean jwtFilterRegistrationBean(){
//        FilterRegistrationBean<Filter> tokenFilterRegistrationBean = new FilterRegistrationBean<>();
//
//        JwtTokenFilter tokenFilter = new JwtTokenFilter();
//        tokenFilterRegistrationBean.setFilter(tokenFilter);
//        tokenFilterRegistrationBean.setOrder(-2);
//        return tokenFilterRegistrationBean;
//    }
}
